//
//  InformationTableView.m
//  Vlink
//
//  Created by 赵隆杰 on 2021/10/9.
//  Copyright © 2021 myTV. All rights reserved.
//

#import "InformationTableView.h"
#import "InformationTableViewCell.h"
#import "InformationDetailViewController.h"

@interface InformationTableView () <UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, assign) BOOL tableCanScroll;

@property (nonatomic, strong) UIView *nodataView;
@property (nonatomic, strong) UIImageView *nodataPic;

@end

@implementation InformationTableView

-(UIView *)nodataView
{
    if (!_nodataView) {
        _nodataView = [UIView new];
        _nodataView.hidden = YES;
        [self addSubview:_nodataView];
        _nodataView.backgroundColor = [UIColor clearColor];
        UIImageView *imgV = [[UIImageView alloc] init];
        imgV.frame = CGRectMake((SL_WINDOW_WIDTH - 303)/2.0, 40, 303, 152);
        imgV.contentMode = UIViewContentModeScaleAspectFit;
        [_nodataView addSubview:imgV];
        _nodataPic = imgV;
    }
    return _nodataView;
}

-(instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        self.tableCanScroll = NO;
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(scrollNotification:) name:@"Information_tableCanScroll" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tableHeaderRefresh:) name:@"Information_tableHeaderRefresh" object:nil];
        [self config];
    }
    return self;
}
-(void)config
{
    self.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    self.backgroundColor = [UIColor clearColor];

    // setContentOffset 不准，加上这里就准了
    self.estimatedRowHeight = 0;
    self.estimatedSectionHeaderHeight = 0;
    self.estimatedSectionFooterHeight = 0;
    self.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SL_WINDOW_WIDTH, CGFLOAT_MIN + 5)];
    
    self.delegate = self;
    self.dataSource = self;
    self.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.estimatedRowHeight = 130;
    self.rowHeight = 130;
    
    [self registerNib:[UINib nibWithNibName:@"InformationTableViewCell" bundle:nil] forCellReuseIdentifier:@"InformationTableViewCell"];
        
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Information_tableCanScroll" object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Information_tableHeaderRefresh" object:nil];
}

-(void)setModelList:(NSArray *)modelList
{
    _modelList = modelList;
        
    // 暂未图
    if (modelList.count) {
        self.nodataView.hidden = YES;
    }else{
        self.nodataView.hidden = NO;
        
    }
    
    // 刷新
    [self reloadData];
    
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    InformationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InformationTableViewCell" forIndexPath:indexPath];
    cell.model = self.modelList[indexPath.row];
    return cell;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    InformationModel *model = self.modelList[indexPath.row];
    InformationDetailViewController *vc = [[InformationDetailViewController alloc] init];
    vc.model = model;
    [[UIWindow currentViewController].navigationController pushViewController:vc animated:YES];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
//    NSLog(@"table -- %.2f", scrollView.contentOffset.y);
    return;
    if (!self.tableCanScroll) {
        [scrollView setContentOffset:CGPointZero];
    }
    
    if (scrollView.contentOffset.y <= 0) {
        _tableCanScroll = NO;
//        NSLog(@"-*- 通知 scrollView 可滑动");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Information_scrollCanScroll" object:nil userInfo:@{@"canScroll":@"1"}];
    }
    
}

#pragma mark - NSNotificationCenter
- (void)scrollNotification:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    NSString *canScroll = userInfo[@"canScroll"];
    if (canScroll.boolValue) {
        _tableCanScroll = YES;
    }
}

- (void)tableHeaderRefresh:(NSNotification *)notification
{
    if (self.tableHeaderReloadBlock) {
        self.tableHeaderReloadBlock();
    }
}

- (void)tableHeaderRefreshFinish
{
//    NSLog(@"-*- 通知 scroll 停止刷新");
    [[NSNotificationCenter defaultCenter] postNotificationName:@"Information_scrollHeaderRefreshStop" object:nil userInfo:nil];
}


@end
